// pages/order/order.js
const app = getApp();
var api = require('../../config/api.js');
const https = require('../../utils/request.js');
const user = require('../../utils/user.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabActive: '0',
    orderList: [],
    stateOptions: [],
    sizeOptions: []
  },
  queryOrder() {
    let that = this;
    wx.showLoading();
    let params = {
      state_id: that.data.tabActive == '0' ? '' : that.data.tabActive,
      openid: wx.getStorageSync('openid'),
      // openid: '02a09f8c5d0ce6020f690e7feacb67ca',
    }
    https.request(api.OrderList, params, 'post').then(res => {
      wx.hideLoading()
      if (res.error_code == 0) {
        console.log('更新订单列表', res.data)
        that.setData({
          orderList: res.data
        })
      }
    }).catch(err => {
      wx.hideLoading()
      console.log('错误', err)
    })
  },

  onTabChange(e) {
    let that = this;
    console.log('点击', e)
    // 更新列表
    let index = e.detail.name;
    that.setData({
      tabActive: index
    });
    that.queryOrder();
  },

  onOrderInfo(e) {
    let item = e.currentTarget.dataset.item;
    let pageType = (item.size_id && item.size_id == null) ? 'single' : 'matrix'
    wx.navigateTo({
      url: '/pages/order/placeOrder?pageType=' + pageType + "&orderId=" + item.order_id,
    })
  },

  async isLoginRegister() {
    let that = this;
    if (app.globalData.hasLogin) {} else {
      let checkReg = await user.checkRegister()
      console.log('验证', checkReg)
      if (!checkReg) {
        wx.navigateBack();
      }
    }
  },

  // 初始化字典
  async initDict() {
    let that = this;
    // 字典
    let status = await https.request(api.OrderStatus, {}, 'post');
    let size = await https.request(api.MatrixSize, {}, 'post');
    console.log('字典1', status)
    console.log('字典2', size)
    if (!(status.data && status.data.length && size.data && size.data.length > 0)) {
      wx.showToast({
        title: '服务器错误',
        icon: 'none'
      })
      return false;
    }
    that.setData({
      stateOptions: status.data.map(res => {
        return {
          code: res.state_id,
          label: res.state_name
        }
      }),
      sizeOptions: size.data.map(res => {
        return {
          code: res.id,
          label: res.size_name
        }
      })
    })
    return true;
  },

  async initFun() {

    let that = this;
    wx.showLoading()
    await that.isLoginRegister();

    let initDict = await that.initDict();
    if (!initDict) {
      return false;
    }

    // console.log('订单状态', status)
    that.queryOrder();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    // this.initFun()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    this.initFun()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})